﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpriteGame.Scenes;
using FarseerPhysics.Dynamics;
using Microsoft.Xna.Framework;
using FarseerPhysics.Dynamics.Contacts;
using SpriteGame.Rendering;

namespace SpriteGame
{
    class DestructableBlock:BaseSprite
    {
        public DestructableBlock()
        {

        }

        public DestructableBlock(Scene _parent, BodyType _type, Vector2 _position, float _rotation, String _texture)
            :base(_parent, _type, _position, _rotation, _texture)
        {

        }

        public override void LoadContent(Scene _parent)
        {
            base.LoadContent(_parent);
            TextureLibrary.LoadTexture(texture + "_Broken");
            body.FixtureList[0].OnSeparation += OnSeparation;
        }

        bool hasBroken = false;

        private void OnSeparation(Fixture fixtureA, Fixture fixtureB)
        {
            if (!hasBroken)
            {
                //Body body1 = fixtureA.Body;
                Body body2 = fixtureB.Body;
                if (body2.UserData is Ball)
                {
                    hasBroken = true;
                    texture = texture + "_Broken";
                    //return false;
                }
                //float mass1 = body1.Mass;
                //float mass2 = body2.Mass;

                //if (mass1 > 0.0f && mass2 > 0.0f)
                //{
                //    if (mass2 > mass1)
                //    {
                //        //parent.RemoveEntity(this);
                //        texture = texture + "_Broken";
                //        hasBroken = true;
                //        return false;
                //    }
                //}
            }
            else
            {
                Body body2 = fixtureB.Body;
                if (body2.UserData is Ball)
                {
                    //parent.AddEntity(new BaseSprite(parent, BodyType.Dynamic, Position, Rand.RandomFloat(0, (float)Math.PI), 0.05f, "Textures/Tile_Broken_Fragment", false));
                    //parent.AddEntity(new BaseSprite(parent, BodyType.Dynamic, Position, Rand.RandomFloat(0, (float)Math.PI), 0.05f, "Textures/Tile_Broken_Fragment", false));
                    //parent.AddEntity(new BaseSprite(parent, BodyType.Dynamic, Position, Rand.RandomFloat(0, (float)Math.PI), 0.05f, "Textures/Tile_Broken_Fragment", false));
                    //parent.AddEntity(new BaseSprite(parent, BodyType.Dynamic, Position, Rand.RandomFloat(0, (float)Math.PI), 0.05f, "Textures/Tile_Broken_Fragment", false));
                    //parent.AddEntity(new BaseSprite(parent, BodyType.Dynamic, Position, Rand.RandomFloat(0, (float)Math.PI), 0.05f, "Textures/Tile_Broken_Fragment", false));
                    //parent.AddEntity(new BaseSprite(parent, BodyType.Dynamic, Position, Rand.RandomFloat(0, (float)Math.PI), 0.05f, "Textures/Tile_Broken_Fragment", false));
                    parent.Particles.CreateExplosion(Position, 50);
                    //AudioManager.SoundManager.PlaySound("Explosion");
                    parent.RemoveEntity(this);
                    //body.FixtureList[0].OnCollision -= OnCollision;
                    body.FixtureList[0].OnSeparation -= OnSeparation;
                    //return false;
                }
            }
            //return true;
        }

        public override void Update(Microsoft.Xna.Framework.GameTime _time)
        {
            //if(parent is MultiplayerGame)
            //{
            //MultiplayerGame game = (MultiplayerGame)parent;
            //    if(game.FollowTarget != null)
            //    {
            //body.FixtureList[0].OnCollision(this, game.FollowTarget, world.conta
            //    }
            //}
            base.Update(_time);
        }
    }
}
